package com.qding.park.common.utils.qiniu;



import com.qiniu.common.QiniuException;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.storage.model.FileListing;

/**   
 *    
 * 项目名称：framework-common   
 * 类名称：BaseService   
 * 类描述：   
 * 创建人：Administrator   
 * 创建时间：2015年6月25日 下午4:15:18   
 * 修改人：Administrator   
 * 修改时间：2015年6月25日 下午4:15:18   
 * 修改备注：   
 * @version    
 *    
 */
public class BaseService {
	private  String bucketName; 
	private BucketManager bucketManager;  //文件管理
	

	/**
	 * 列出某一个空间bucket下的所有文件
	 * 
	 * @param key
	 * @param limit
	 * @return
	 * @throws QiniuException
	 */
	public FileListing ListFiles(int limit) throws QiniuException {
		FileListing filelists = null;
		try {
			filelists = bucketManager.listFiles(this.bucketName, null, null,
					limit, null);
		} catch (QiniuException e) {
			throw new QiniuException(e);
		}
		return filelists;
	}

	/**
	 * 查看bucket空间中某个图片key的详细信息
	 * 
	 * @param bucket
	 * @param key
	 * @return
	 * @throws QiniuException
	 */
	public FileInfo FileStat(String key) throws QiniuException {
		FileInfo info = null;
		try {
			info = bucketManager.stat(bucketName, key);
		} catch (QiniuException e) {
			throw new QiniuException(e);
		}
		return info;
	}

	/**
	 * 删除bucket空间中某个图片key
	 * 
	 * @param bucket
	 * @param key
	 * @return
	 * @throws QiniuException
	 */
	public void FileDelete(String key) throws QiniuException {
		try {
			bucketManager.delete(bucketName, key);
		} catch (QiniuException e) {
			throw new QiniuException(e);
		}
	}

	/**
	 * 文件复制
	 * 
	 * @param from_bucket
	 * @param from_key
	 * @param to_bucket
	 * @param to_key
	 * @throws QiniuException
	 */
	public void FileCopy(String from_bucket, String from_key, String to_bucket,
			String to_key) throws QiniuException {
		try {
			bucketManager.copy(from_bucket, from_key, to_bucket, to_key);
		} catch (QiniuException e) {
			throw new QiniuException(e);
		}
	}

	/**
	 * 重新命名bucket空间中某个图片key
	 * 
	 * @param bucket
	 * @param key
	 * @return
	 * @throws QiniuException
	 */
	public void FileRename(String oldname, String newname)
			throws QiniuException {
		try {
			bucketManager.rename(this.bucketName, oldname, newname);
		} catch (QiniuException e) {
			throw new QiniuException(e);
		}
	}

	/**
	 * 修改文件类型
	 * 
	 * @param bucket
	 * @param key
	 * @param mime
	 * @throws QiniuException
	 */
	public void FileChangeMime(String key, String mime) throws QiniuException {
		try {
			bucketManager.changeMime(this.bucketName, key, mime);
		} catch (QiniuException e) {
			throw new QiniuException(e);
		}
	}

	/**
	 * 从镜像更新图片
	 * 
	 * @param bucket
	 * @param key
	 * @throws QiniuException
	 */
	public void Prefetch(String key) throws QiniuException {
		try {
			bucketManager.prefetch(this.bucketName, key);
		} catch (QiniuException e) {
			throw new QiniuException(e);
		}
	}

	/**
	 * 从指定的URL抓取图片存储到key中
	 * 
	 * @param url
	 * @param bucket
	 * @param key
	 * @throws QiniuException
	 */
	public void Fetch(String url, String key) throws QiniuException {
		try {
			bucketManager.fetch(url, this.bucketName, key);
		} catch (QiniuException e) {
			throw e;
		}
	}

	public String getBucketName() {
		return bucketName;
	}

	public void setBucketName(String bucketName) {
		this.bucketName = bucketName;
	}

	public BucketManager getBucketManager() {
		return bucketManager;
	}

	public void setBucketManager(BucketManager bucketManager) {
		this.bucketManager = bucketManager;
	}

	
}
